package arithmetic2.test;

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

class Solution {
    /**
     * 3 2
     * 1 2
     * 3 1
     * 1 4
     *
     * @param array
     * @return
     */
    /* Write Code Here */
    public int pathSum(int[][] array) {
        //建立一个缓存
        int[][] buffer = new int[array.length][array[0].length];

        int max = Integer.MIN_VALUE;
        for (int i = 0; i < array[0].length; i++) {
            int value = m(array, 0, i, 0, buffer);
            if (max < value) {
                max = value;
            }
        }
        return max;
    }

    //计算从某个点出发得到最大的数
    public int m(int[][] array, int x, int y, int sum, int[][] buffer) {
        //判断缓存是否存在
        if (buffer[x][y] != 0) {
            return buffer[x][y];
        }

        //可以往三个方向走
        if (x < array.length - 1 && y > 0 && y < array[0].length - 1) {
            sum = array[x][y] +
                    max(m(array, x + 1, y - 1, sum, buffer),
                            m(array, x + 1, y, sum, buffer),
                            m(array, x + 1, y + 1, sum, buffer));
            buffer[x][y] = sum;
            return sum;
        } else if (array[0].length == 1 && x < array.length - 1) {
            //只能往下走
            sum = array[x][y] + m(array, x + 1, y, sum, buffer);
            buffer[x][y] = sum;
            return sum;
        } else if (y == 0 && x < array.length - 1) {
            //只能往下和右下走
            sum = array[x][y] + Math.max(m(array, x + 1, y, sum, buffer),
                    m(array, x + 1, y + 1, sum, buffer));
            buffer[x][y] = sum;
            return sum;
        } else if (y == array[0].length - 1 && x < array.length - 1) {
            //只能往左下和下走
            sum = array[x][y] + Math.max(m(array, x + 1, y - 1, sum, buffer),
                    m(array, x + 1, y, sum, buffer));
            buffer[x][y] = sum;
            return sum;
        }

        //没有路可以走,返回当前值
        buffer[x][y] = array[x][y];
        return array[x][y];
    }

    //获取三个数中最大的数
    public int max(int a, int b, int c) {
        return Math.max(Math.max(a, b), c);
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int res;

        int array_rows = 0;
        int array_cols = 0;
        array_rows = in.nextInt();
        array_cols = in.nextInt();

        int[][] array = new int[array_rows][array_cols];
        for (int array_i = 0; array_i < array_rows; array_i++) {
            for (int array_j = 0; array_j < array_cols; array_j++) {
                array[array_i][array_j] = in.nextInt();
            }
        }

        if (in.hasNextLine()) {
            in.nextLine();
        }


        res = new Solution().pathSum(array);
        System.out.println(String.valueOf(res));

    }
}
